package com.example.controller;

import com.example.common.IdWorker;
import com.example.common.R;
import com.example.domain.ShoppingCart;
import com.example.service.ShoppingCartService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@ResponseBody
@RequestMapping("shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpSession session)
    {
        Long user = (Long) session.getAttribute("user");
        List<ShoppingCart> selectall = shoppingCartService.selectall(user);
        return R.success(selectall);
    }

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpSession session)
    {
        if(shoppingCart.getSetmealId()!=null)
        {
            Long user = (Long) session.getAttribute("user");
            String name = shoppingCart.getName();
            ShoppingCart selectbyname = shoppingCartService.selectbyname(name,user);
            if(selectbyname==null)
            {
                log.info("into selectbyname==null");
                IdWorker idWorker=new IdWorker();
                long id = idWorker.nextId();
                shoppingCart.setId(id);
                shoppingCart.setUserId(user);
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartService.savethesetmeal(shoppingCart);
            }
            else
            {
                int getnumberbyname = shoppingCartService.getnumberbyname(name, user);
                getnumberbyname++;
                String name1 = selectbyname.getName();
                shoppingCartService.updatenumber(getnumberbyname,user,name1);
            }
            return R.success(shoppingCart);
        }
        else
        {
            Long user = (Long) session.getAttribute("user");
            String name = shoppingCart.getName();
            ShoppingCart selectbyname = shoppingCartService.selectbyname(name, user);
            if(selectbyname==null)
            {
                IdWorker idWorker=new IdWorker();
                long id = idWorker.nextId();
                shoppingCart.setId(id);
                shoppingCart.setUserId(user);
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartService.savethedish(shoppingCart);
            }
            else
            {
                int getnumberbyname = shoppingCartService.getnumberbyname(name, user);
                getnumberbyname++;
                String name1 = selectbyname.getName();
                shoppingCartService.updatenumber(getnumberbyname,user,name1);
            }
            return R.success(shoppingCart);
        }
    }

    @PostMapping("/sub")
    public R<ShoppingCart> delete(@RequestBody ShoppingCart shoppingCart,HttpSession session)
    {
        Long user = (Long) session.getAttribute("user");
        Long setmealId = shoppingCart.getSetmealId();
        Long dishId = shoppingCart.getDishId();
        if(setmealId!=null)//删除setmeal
        {
            ShoppingCart sgetnumberbysetmeal = shoppingCartService.sgetnumberbysetmeal(setmealId, user);
            Integer number = sgetnumberbysetmeal.getNumber();
            String name = sgetnumberbysetmeal.getName();
            if(number>1)
            {
                number--;
                shoppingCartService.updatenumber(number,user,name);
            }
            else
            {
                shoppingCartService.deletebyname(name,user);
            }
            return R.success(shoppingCart);
        }
        else//删除dish
        {
            ShoppingCart getnumberbydish = shoppingCartService.getnumberbydish(dishId, user);
            Integer number = getnumberbydish.getNumber();
            String name = getnumberbydish.getName();
            if(number>1)
            {
                number--;
                shoppingCartService.updatenumber(number,user,name);
            }
            else
            {
                shoppingCartService.deletebyname(name,user);
            }
            return R.success(shoppingCart);
        }
    }

    @DeleteMapping("/clean")
    public R<String> clean(HttpSession session)
    {
        Long user = (Long) session.getAttribute("user");
        shoppingCartService.clean(user);
        return R.success("已清空购物车");
    }
}
